package com.examples.helloTimePicker;

import java.util.Calendar;

import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;

public class HelloTimePicker extends Activity {
	
	private TextView mTimeDisplay;
	private Button mPickerTime;
	
	private int mHour;
	private int mMinute;
	
	static final int TIME_DIALOG_ID = 0;
	
	private OnTimeSetListener mTimeSetListener = new OnTimeSetListener() {
		
		@Override
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

			mHour = hourOfDay;
			mMinute = minute;
			
			updateLayout();
		}
	};
	
    private void updateLayout(){
    	StringBuilder text = new StringBuilder();
    	
    	text.append(pad(mHour)).append(":");
    	text.append(pad(mMinute));
    	
    	mTimeDisplay.setText(text);    	
    }
	
    private static String pad(int c){
    	if (c >= 10) {
			return String.valueOf(c);
		} else {
			return "0" + String.valueOf(c);
		}
    }
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mTimeDisplay = (TextView) findViewById(R.id.timeDisplay);
        mPickerTime = (Button) findViewById(R.id.pickTime);
        
        mPickerTime.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				showDialog(TIME_DIALOG_ID);				
			}
		});
        
        final Calendar c = Calendar.getInstance();
        mHour = c.get(Calendar.HOUR);
        mMinute = c.get(Calendar.MINUTE);
        
        updateLayout();
    }
        
    protected Dialog onCreateDialog(int id) {
    	
		switch (id) {
			case TIME_DIALOG_ID:
				return new TimePickerDialog(this, mTimeSetListener, mHour, mMinute, true);
			default:
				break;
		}
		
		return null;
	}
}